2 Experimenting wiht the input...
14 const int BUFSIZE
= 512;
17 #define D(x) cout << #x " is " << (x) << endl
19 typedef unsigned long long uint64
;
25 node(bool end
) : end(end
) {
29 if (left
== NULL
) left
= new node(false);
33 if (right
== NULL
) right
= new node(false);
39 inline uint64
pow2(int e
){
40 if (e
== 64) return 0;
41 return uint64(1) << e
;
44 void clean(node
* cur
){
45 if (cur
== NULL
) return;
54 Returns how many strings were counted under the tree rooted at cur, including it.
56 uint64
alreadyCounted(node
* cur
, int depth
= 0){
57 if (cur
== NULL
) return 0;
58 uint64 left
= alreadyCounted(cur
->left
, depth
+ 1);
59 uint64 right
= alreadyCounted(cur
->right
, depth
+ 1);
60 uint64 ret
= left
+ right
;
62 //count strings matched to this one
63 ret
+= pow2(m
- depth
) - left
- right
;
71 scanf("%d%d\n", &n
, &m
);
72 if (n
== 0 && m
== 0) break;
73 node
* root
= new node(true);
74 for (int i
=0; i
<n
; ++i
){
84 next
= cur
->getLeft();
85 }else if (buf
[j
] == '1'){
86 next
= cur
->getRight();
88 printf("Bad character %c at query %d, position %d\n", buf
[j
], i
, j
);
96 for (int i
=0; i
<k
; ++i
){
99 for (int j
=0; buf
[j
] != '*'; ++j
){
102 }else if (buf
[j
] == '1'){
107 s
= s
.substr(0, s
.size()-1);
109 uint64 ans
= pow2(m
- s
.size())
110 - alreadyCounted(cur
->left
, s
.size()+1) - alreadyCounted(cur
->right
, s
.size()+1);
111 printf("%llu\n", ans
);